﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalcSharp.Core.Containers;
using System.Drawing;
using CalcSharp.Core;

namespace plug_mtrximage.HelperClasses
{
    class DisplacementFilters
    {
        public static AMatrix OffsetFilter(AMatrix input, Point[,] offset, MtrxStorageType t)
        {
            AMatrix ret;
            int xOffset, yOffset;
            Int24 tmp = new Int24();
            switch (t)
            {
                case MtrxStorageType.tdouble:
                    ret = new DoubleMatrix(input.Rows, input.Columns);
                    break;
                case MtrxStorageType.tfloat:
                    ret = new FloatMatrix(input.Rows, input.Columns);
                    break;
                case MtrxStorageType.tint24:
                    ret = new Int24Matrix(input.Rows, input.Columns);
                    break;
                default:
                    ret = new DoubleMatrix(input.Rows, input.Columns);
                    break;
            }

            for (int i = 0; i < ret.Rows; i++) //y
            {
                for (int j = 0; j < ret.Columns; j++) //x
                {
                    xOffset = offset[j, i].X;
                    yOffset = offset[j, i].Y;
                    if (i + yOffset >= 0 && i + yOffset < ret.Rows && j + xOffset >= 0 && j + xOffset < ret.Columns)
                    {
                        tmp.FromInt32((int)input[(i + yOffset), (j + xOffset)]);
                        ret[i, j] = tmp.ToInt32();
                    }
                }
            }

            return ret;
        }
    }
}
